package com.itrip.service.impl;

import com.itrip.dao.ItripHotelOrderMapper;
import com.itrip.dao.ex.OrderMapperEX;
import com.itrip.dao.ex.TempStoreMapperEX;
import com.itrip.dto.OrderListDTO;
import com.itrip.dto.OrderPayDTO;
import com.itrip.pojo.ItripHotelOrder;
import com.itrip.service.OrderService;
import com.itrip.vo.order.OrderCountVO;
import com.itrip.vo.order.OrderListVO;
import com.itrip.vo.order.PayStatusVO;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class OrderServiceImpl implements OrderService {

    @Resource
    private ItripHotelOrderMapper orderMapper;

    @Resource
    private OrderMapperEX orderMapperEX;

    @Resource
    private TempStoreMapperEX tempStoreMapperEX;

    @Resource
    private RedisTemplate<String,Object> redisTemplate;
    @Override
    @Transactional(rollbackFor = Exception.class)
    public int addOrder(ItripHotelOrder order) {

        int countOrder = orderMapper.insertSelective(order);
        int countStore = 0;
        if (countOrder > 0){
            int storeSub = order.getCount();
            Long roomId = order.getRoomId();
            countStore = tempStoreMapperEX.addStore(roomId,storeSub);
            redisTemplate.opsForValue().set("order:"+order.getId()+","
                    +order.getRoomId()+","+storeSub,order.getUserId(),30, TimeUnit.MINUTES);
        }
        return countStore;
    }

    @Override
    public int getCountByUser(Long userId) {
        return orderMapperEX.getCountByUser(userId);
    }

    @Override
    public OrderPayDTO getOrderPayByUser(Long userId) {
        return orderMapperEX.getOrderPayByUser(userId);    }

    @Override
    public int updateOrderStatus(PayStatusVO payStatusVO) {

        return orderMapperEX.updateOrderStatus(payStatusVO);
    }

    @Override
    public List<OrderListDTO> getOrderListByParams(OrderListVO orderListVO, Long userId, int start) {
        return orderMapperEX.getOrderListByParams(orderListVO,userId,start);
    }

    @Override
    public int getOrderCountByParams(OrderCountVO orderCountVO, Long userId) {
        return orderMapperEX.getOrderCountByParams(orderCountVO,userId);
    }

    @Override
    public int getOrderStatus(long orderId) {
        return orderMapperEX.getOrderStatus(orderId);
    }

    @Override
    public int updateOrderStatusInvalid(long orderId) {
        return orderMapperEX.updateOrderStatusInvalid(orderId);
    }


}
